package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.util.base.CommonUtil;
import common.util.base.Result;
import common.util.mybatis.plus.layui.Page;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import java.util.Map;
</#if>
<#if superControllerClassPackage??>

import ${superControllerClassPackage};
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Slf4j
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass}<${table.serviceName}, ${entity}>{
<#else>
public class ${table.controllerName} {
</#if>
<#if !restControllerStyle >

    @Autowired
    private ${table.serviceName} i${entity}Service;

    /**
     * 跳转的列表页面
     * @return 跳转页面
     */
    @RequestMapping("/listPage")
    public Object listPage() {
      return "${table.entityPath}_list";
    }

    /**
     * 列表页面表格数据
     * @param entity 实体对象
     * @param page 分页对象
     * @return page 分页对象
     */
    @ResponseBody
    @RequestMapping("/listData")
    public Object listData(${entity} entity, Page<${entity}> page) {
        QueryWrapper<${entity}> query= new QueryWrapper<>();
        if(CommonUtil.isNotEmpty(entity)){
            Map<String,Object> entityMap = CommonUtil.convertBeanToMap(entity);
            for (String key:entityMap.keySet()){
                if(CommonUtil.isNotEmpty(entityMap.get(key))){
                    query.like(key,entityMap.get(key));
                }
            }
        }
        return Result.success(i${entity}Service.page(page, query));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/editPage")
    public Object editPage(String id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            ${entity} entity = this.i${entity}Service.getById(id);
            if(CommonUtil.isNotEmpty(entity)) {
                model.addAttribute("entity",entity);
            }
        }
        return "${table.entityPath}_edit";
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/save")
    public Object save(${entity} entity) {
        log.info("开始保存数据:{}" ,entity);
        this.i${entity}Service.saveOrUpdate(entity);
        return Result.success();
    }

    /**
     * 数据删除
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/removed")
    public Object removed(${entity} entity) {
        this.i${entity}Service.removeById(entity.getId());
        return Result.success();
    }
</#if>

}
</#if>
